Imports DAL
Imports MyClasses
Imports System.Configuration

Public Class frmTab
    Public PrefID As Integer
    Public TabID As String
    Dim cnt As Integer
    Private ScrnCtrl() As Object
    Private labels() As Label
    Dim objTab(0) As CTab

    Private Sub frmTab_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Application Settings")
        e.Cancel = True
        'Help.ShowHelp(Me, "D:\ICS_CODE\PHARMACY\BIN\DEBUG\HELP\Pharma.chm", HelpNavigator.Index, 90)
    End Sub

    Private Sub frmTab_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call DisplayTab()
        Call SetControls()
        Call AddIntHandlers(cnt)
    End Sub

    Private Sub frmTab_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If isUpdate() Then
                    Select Case BCommon.ShowMsgError(10081, "YNC")
                        Case Windows.Forms.DialogResult.Yes
                            If ValidateData() Then
                                Call UpdateData()
                            Else
                                e.Cancel = True
                            End If
                        Case Windows.Forms.DialogResult.Cancel
                            e.Cancel = True
                        Case Windows.Forms.DialogResult.No
                    End Select
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmTab_FormClosing")
        End Try
    End Sub

    Public Function isUpdate() As Boolean
        Dim dtControls As New DataTable
        Dim count As Integer
        'Dim dt As New DataTable
        Try
            dtControls = BTab.GetControls()
            For count = 0 To dtControls.Rows.Count - 1
                If dtControls.Rows(count).Item("PrefCtl") = "TXT" Then
                    If ScrnCtrl(count).Text <> dtControls.Rows(count).Item("PrefValue") Then
                        Return True
                    End If
                ElseIf dtControls.Rows(count).Item("PrefCtl") = "DDL" Then
                    If ScrnCtrl(count).SelectedValue <> dtControls.Rows(count).Item("PrefValue") Then
                        Return True
                    End If
                End If
            Next
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
        End Try
        'Return True
    End Function

    Public Sub DisplayTab()
        Dim dtTab As New DataTable
        Dim count As Integer

        Try
            BCommon.SetFormStandards(Me)
            TabCntrl.Enabled = True
            dtTab = BTab.GetTab()
            If dtTab.Rows.Count > 0 Then
                For count = 0 To dtTab.Rows.Count - 1
                    TabCntrl.TabPages.Add(dtTab.Rows(count).Item("TabId"), dtTab.Rows(count).Item("TabNm"))
                    TabCntrl.TabPages(count).BackColor = Color.AliceBlue
                Next
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "DisplayTab")
        End Try
    End Sub

    Private Sub SetControls()
        Dim intX As Integer = 32 'Starting X position for ItemCombo
        Dim intY As Integer = 25 'Starting Y position for ItemCombo
        Dim Fnt As Font = New Font("Microsoft Sans Serif", 9)
        Dim intCtrlWidth As Integer = 148 'Control width
        Dim intLblWidth As Integer = 200  'Control width
        Dim intCtrlHeight As Integer = 20
        Dim intCount As Integer = 0
        Dim dtGetControls As New DataTable
        Dim drGetControls As DataRow
        Dim intHspace As Integer = 50, intVspace As Integer = 26
        Dim tempVar As Integer = 0
        Dim ToolTipNormal As New ToolTip
        Dim strFormMode As String = ConfigurationManager.AppSettings("FormMode")
        Try
            dtGetControls = BTab.GetControls()
            ReDim labels(dtGetControls.Rows.Count)
            ReDim ScrnCtrl(dtGetControls.Rows.Count)

            If dtGetControls.Rows.Count > 0 Then
                For intCount = 0 To dtGetControls.Rows.Count - 1
                    For Each drGetControls In dtGetControls.Rows
                        If tempVar = 0 Then
                            tempVar = drGetControls.Item("TabId")
                            intY = 25
                        ElseIf tempVar <> drGetControls.Item("TabId") Then
                            tempVar = drGetControls.Item("TabId")
                            intY = 25
                        Else
                            intY = intY + intVspace
                        End If

                        labels(intCount) = New Label()
                        labels(intCount).Visible = True
                        labels(intCount).Parent = TabCntrl.TabPages(drGetControls.Item("TabID") - 1)
                        labels(intCount).Width = intLblWidth
                        labels(intCount).Text = BCommon.ChkNullVal(drGetControls("PrefDesc"), "")
                        labels(intCount).Location = New Point(intX, intY)
                        labels(intCount).Font = Fnt
                        If strFormMode = "R" Then
                            labels(intCount).Enabled = False
                        End If
                        If BCommon.ChkNullVal(drGetControls("PrefCtl"), "") = "DDL" Then
                            ScrnCtrl(intCount) = New ComboBox()
                            ScrnCtrl(intCount).Visible = True
                            ScrnCtrl(intCount).Parent = TabCntrl.TabPages(drGetControls.Item("TabID") - 1)
                            ScrnCtrl(intCount).Size = New Point(intCtrlWidth, intCtrlHeight)
                            ScrnCtrl(intCount).Location = New Point(intX + intLblWidth + intHspace, intY)
                            ScrnCtrl(intCount).Font = Fnt
                            ScrnCtrl(intCount).DropDownStyle = ComboBoxStyle.DropDownList
                            If strFormMode = "R" Then
                                ScrnCtrl(intCount).Enabled = False
                            End If
                            BCommon.SetComboValWithIndex(ScrnCtrl(intCount), BTab.GetSelectRecord(drGetControls("PrefSrcQry").ToString))
                            If drGetControls.Item("PrefValue") <> "" Or drGetControls.Item("PrefValue") <> Nothing Then
                                ScrnCtrl(intCount).SelectedValue() = drGetControls.Item("PrefValue")
                            End If
                            If Not drGetControls.IsNull("tooltip") Then
                                ToolTipNormal.SetToolTip(ScrnCtrl(intCount), drGetControls.Item("tooltip"))
                            End If
                        End If

                        If BCommon.ChkNullVal(drGetControls("PrefCtl"), "") = "TXT" Then
                            ScrnCtrl(intCount) = New TextBox()
                            ScrnCtrl(intCount).Visible = True
                            ScrnCtrl(intCount).Parent = TabCntrl.TabPages(drGetControls.Item("TabID") - 1)
                            ScrnCtrl(intCount).Size = New Point(intCtrlWidth, intCtrlHeight)
                            ScrnCtrl(intCount).Location = New Point(intX + intLblWidth + intHspace, intY)
                            ScrnCtrl(intCount).Font = Fnt
                            ScrnCtrl(intCount).MaxLength = dtGetControls.Rows(intCount).Item("PrefCtlMaxlen")
                            ScrnCtrl(intCount).Text = drGetControls.Item("PrefValue")
                            If strFormMode = "R" Then
                                ScrnCtrl(intCount).Enabled = False
                            End If
                            If Not drGetControls.IsNull("tooltip") Then
                                ToolTipNormal.SetToolTip(ScrnCtrl(intCount), drGetControls.Item("tooltip"))
                            End If
                        End If
                        intCount = intCount + 1
                    Next
                Next
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetControls")
        End Try
    End Sub

    ' --- HANDLER used for Validation for Numbers
    Private Sub AddIntHandlers(ByVal count As Integer)
        Dim dtControls As New DataTable
        Try
            dtControls = BTab.GetControls()
            If dtControls.Rows.Count > 0 Then
                For count = 0 To dtControls.Rows.Count - 1
                    Select Case dtControls.Rows(count).Item("PrefCtl")
                        Case "TXT"
                            If dtControls.Rows(count).Item("PrefDataType") = "INT" Then
                                AddHandler CType(ScrnCtrl(count), System.Windows.Forms.TextBox).KeyDown, AddressOf ScrnCtrl_KeyDown
                            End If
                            'Rahul_ITS@20072412 : Add following case for DropDown to add key down hadler
                        Case "DDL"
                            AddHandler CType(ScrnCtrl(count), System.Windows.Forms.ComboBox).KeyDown, AddressOf ScrnCtrl_KeyDown
                    End Select
                Next
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "AddComboHandlers")
        End Try
    End Sub

        Private Sub ScrnCtrl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            If e.Alt Then Exit Sub
            'Rahul_ITS@20072412 : Add following single line
            If e.KeyCode = Keys.Enter Then  SendKeys.Send("{TAB}")
            Select Case e.KeyCode
                Case 222
                    e.SuppressKeyPress = True : Exit Sub
                Case Keys.Back, Keys.Delete, Keys.Tab, Keys.Home, Keys.End, Keys.Enter, Keys.Up, Keys.Left, Keys.Right, Keys.Down, Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, Keys.NumPad4, Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, Keys.NumPad9
                    'Case Keys.Decimal, Keys.OemPeriod
                    '    If sender.Text.IndexOf(".") >= 0 Then e.SuppressKeyPress = True : Exit Sub
                Case Else
                    e.SuppressKeyPress = True : Exit Sub
            End Select
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ScrnCtrl_Click")
        End Try
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Call UpdateData()
    End Sub

    Private Sub UpdateData()
        Dim dtGetControls As New DataTable
        Dim count As Integer
        'Dim config As System.Configuration.Configuration
        Try
            'config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)  'Open App.Config of executable
            dtGetControls = BTab.GetControls()
            If ValidateData() = False Then Exit Sub

            For count = 0 To dtGetControls.Rows.Count - 1
                If dtGetControls.Rows(count).Item("KeyName") <> "" Then
                    ConfigurationManager.AppSettings.Set(dtGetControls.Rows(count).Item("KeyName"), ScrnCtrl(count).Text)
                    'config.Save(ConfigurationSaveMode.Modified)
                    'ConfigurationManager.RefreshSection("AppSettings")
                End If
            Next
            BTab.Update(SetData(), PrefID)
            BCommon.ShowMsgError(5)
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
        End Try
    End Sub

    Private Function SetData() As CTab()
        Try
            Dim dtControls As New DataTable
            Dim intCount As Integer
            dtControls = BTab.GetControls()
            ReDim objTab(dtControls.Rows.Count)

            For intCount = 0 To dtControls.Rows.Count - 1
                objTab(intCount) = New CTab
                If dtControls.Rows(intCount).Item("PrefCtl") = "DDL" Then
                    objTab(intCount).PrefValue = ScrnCtrl(intCount).SelectedValue
                Else
                    objTab(intCount).PrefValue = ScrnCtrl(intCount).Text.ToString()
                End If
            Next
            Return objTab
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Function ValidateData() As Boolean
        Dim count As Integer
        Dim dtControls As New DataTable
        Dim intVar, intMinVal, intMaxVal As Integer
        Dim strVar As String
        Dim strMsg As String = String.Empty
        Try
            dtControls = BTab.GetControls()
            If dtControls.Rows.Count > 0 Then
                For count = 0 To dtControls.Rows.Count - 1
                    If BCommon.ChkNullVal(dtControls.Rows(count).Item("PrefCtl"), "") = "TXT" Then
                        If dtControls.Rows(count).Item("Compulsory_yn") = "Y" Then  'Validating control should not be blank
                            If ScrnCtrl(count).Text = "" Then
                                strMsg = BCommon.GetMessage(10255)
                                strMsg.Replace("@Value!", Replace(labels(count).Text, "&", "") & ".")
                                BCommon.ShowMessage(strMsg)
                                ScrnCtrl(count).Focus()
                                Return False
                            End If
                        End If
                        If dtControls.Rows(count).Item("PrefDataType") = "INT" Then  'Validation for numbers
                            'If (BCommon.IsInteger(ScrnCtrl(count).Text) = False) Then ScrnCtrl(count).Focus() : ScrnCtrl(count).Text = ""
                            intVar = ScrnCtrl(count).Text
                            intMinVal = Convert.ToInt32(dtControls.Rows(count).Item("PrefMinVal"))
                            intMaxVal = Convert.ToInt32(dtControls.Rows(count).Item("PrefMaxVal"))
                            If intVar < intMinVal Or intVar > intMaxVal Then          'Validation for MaxValue & MinValue entered in the TextBox 
                                strMsg = String.Empty
                                strMsg = BCommon.GetMessage(10397)
                                strMsg = Replace(strMsg, "@MinVal", intMinVal)
                                strMsg = Replace(strMsg, "@MaxVal", intMaxVal)
                                BCommon.ShowMessage(strMsg)
                                ScrnCtrl(count).Focus()
                                ScrnCtrl(count).Text = ""
                                Return False
                            End If
                        End If
                        'Validation to search company by Code or by Name
                        If dtControls.Rows(count).Item("PrefName") = "COMPANY SEARCH BY" Then
                            strVar = UCase(ScrnCtrl(count).Text)
                            If strVar <> "N" Then
                                If strVar <> "C" Then
                                    BCommon.ShowMsgError(10256)
                                    Return False
                                End If
                            End If
                            If strVar <> "C" Then
                                If strVar <> "N" Then
                                    BCommon.ShowMsgError(10256)
                                    Return False
                                End If
                            End If
                        End If
                    End If
                Next
            End If

            Return True

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Return Nothing
        End Try
        Return True
    End Function

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim dtControls As New DataTable
        Dim count As Integer
        Try
            dtControls = BTab.GetControls()
            If dtControls.Rows.Count > 0 Then
                For count = 0 To dtControls.Rows.Count - 1
                    If dtControls.Rows(count).Item("PrefValue") <> "" Or dtControls.Rows(count).Item("PrefValue") <> Nothing Then
                        ScrnCtrl(count).Text = dtControls.Rows(count).Item("PrefValue")
                    End If
                Next
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnCancel_Click")
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub TabCntrl_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabCntrl.DrawItem
        Dim CurrentTab As TabPage = TabCntrl.TabPages(e.Index)
        Dim ItemRect As Rectangle = TabCntrl.GetTabRect(e.Index)
        Dim FillBrush As New SolidBrush(Color.Red)
        Dim TextBrush As New SolidBrush(Color.White)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center

        'If we are currently painting the Selected TaDItem we'll 
        'change the brush colors and inflate the rectangle.
        If CBool(e.State And DrawItemState.Selected) Then
            FillBrush.Color = Color.White
            TextBrush.Color = Color.Red
            ItemRect.Inflate(2, 2)
        End If

        'Set up rotation for left and right aligned tabs
        If TabCntrl.Alignment = TabAlignment.Left Or TabCntrl.Alignment = TabAlignment.Right Then
            Dim RotateAngle As Single = 90
            If TabCntrl.Alignment = TabAlignment.Left Then RotateAngle = 270
            Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 2))
            e.Graphics.TranslateTransform(cp.X, cp.Y)
            e.Graphics.RotateTransform(RotateAngle)
            ItemRect = New Rectangle(-(ItemRect.Height \ 2), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
        End If

        'Next we'll paint the TaDItem with our Fill Brush
        e.Graphics.FillRectangle(FillBrush, ItemRect)

        'Now draw the text.
        e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, RectangleF.op_Implicit(ItemRect), sf)

        'Reset any Graphics rotation
        e.Graphics.ResetTransform()

        'Finally, we should Dispose of our brushes.
        FillBrush.Dispose()
        TextBrush.Dispose()

    End Sub

    Private Sub frmTab_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Application Settings")
        End If
    End Sub
End Class